#include "controller.h"

Controller::Controller(QObject *parent) :
    QObject(parent)
{
    hotkey = new QHotkey{QKeySequence("Alt+R"), true};
    trayicon = new QSystemTrayIcon{QIcon(":/icon/launch")};
    launchbar = new LaunchBar{};
    executor = new Executor{};

    executor->linkList<<":dir"<<":quit"<<":reload";

    model.setStringList(executor->linkList);

    launchbar->setEditCompleterModel(model);

    setupTrayicon();
    setupConnections();

    trayicon->show();
    reload();
}

Controller::~Controller()
{
    delete menu;
    delete executor;
    delete launchbar;
    delete trayicon;
    delete hotkey;
}

void Controller::setupTrayicon()
{
    menu = new QMenu();
    showHideAction = new QAction{"show/hide", menu};
    reloadAction = new QAction{"reload", menu};
    quitAction = new QAction{"quit", menu};
    if(launchbar->completerSensitiveToggle() == Qt::CaseSensitive)
        caseSensitiveAction = new QAction{"CaseSensitive",menu};
    else
        caseSensitiveAction = new QAction{"CaseInsensitive",menu};

    //todo: add shortcut
//    showHideAction->setShortcut(QKeySequence(Qt::Key::Key_Escape));
//    reloadAction->setShortcut(QKeySequence(Qt::Key::Key_F5));

    menu->addAction(showHideAction);
    menu->addAction(reloadAction);
    menu->addAction(quitAction);
    menu->addAction(caseSensitiveAction);

    trayicon->setContextMenu(menu);
}

void Controller::setupConnections()
{
    connect(hotkey, &QHotkey::activated,
            launchbar, &LaunchBar::showHide);
    connect(launchbar, &LaunchBar::execute,
            executor, &Executor::execute);
    connect(executor, &Executor::executeInnerAction,
            this, &Controller::executeInnerAction);
    connect(showHideAction, &QAction::triggered,
            launchbar, &LaunchBar::showHide);
    connect(reloadAction, &QAction::triggered,
            this, &Controller::reload);
    connect(quitAction, &QAction::triggered,
            this, &Controller::quit);
    connect(trayicon, &QSystemTrayIcon::activated,
            launchbar, &LaunchBar::showHide);
    connect(caseSensitiveAction,&QAction::triggered,
            launchbar, [=]()
    {
        if(launchbar->completerSensitiveToggle() == Qt::CaseSensitive)
        {
            caseSensitiveAction->setText("CaseSensitive");
        }
        else
        {
            caseSensitiveAction->setText("CaseInsensitive");
        }
    });
}

void Controller::executeInnerAction(const QString &action)
{
    if (action == ":quit")
    {
        quit();
    }
    else if(action == ":dir")
    {
        showLinksDir();
    }
    else if(action == ":reload")
    {
        reload();
    }
}

void Controller::showLinksDir()
{
    executor->absoluteExecute(executor->linksDir());
}

void Controller::reload()
{
    executor->reload();
    trayicon->showMessage("Done","MyLauncher has been reloaded.");
    executor->linkList<<":dir"<<":quit"<<":reload";
    model.setStringList(executor->linkList);
    launchbar->setEditCompleterModel(model);
}

void Controller::quit()
{
    qApp->quit();
}
